const gulp = require('gulp');
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const handleErrors = require('../utils/handleErrors.js');
const sourcemaps = require('gulp-sourcemaps');
const globby = require('globby');

gulp.task('browserify', function( cb ) {
	globby(['./app/assets/js/**/*.js','!./app/assets/js/components/**/*']).then(function (paths) {
		console.log(paths);
		paths.forEach(function ( path ) {
			browserify({
				debug: true,
				entries: path
			}).bundle()
			.on('error',handleErrors)
			.pipe(source(path.split(/.*\//)[1]))
			.pipe(gulp.dest('./.temp/assets/js'));
		});
		
		cb();
	});
})
// gulp.task('browserify', ['browserify-lib'], function() {
// 	var b  = browserify({
// 		debug: true,
// 		// noParse: [
// 		// 	require.resolve('./app/assets/lib/jquery-1.12.1.min.js')
// 		// ],
// 		entries: ['./app/assets/js/index.js','./app/assets/js/modules-mgr.js']
// 	}).ignore('./app/assets/lib/jquery-1.12.1.min.js');

// 	//return browserify(['./app/assets/js/index.js','./app/assets/js/modules-mgr.js'])
// 	return b.bundle()
// 		//.on('error', handleErrors)
// 		.pipe(source('index.js'))
// 		.pipe(gulp.dest('./.temp/assets/js'));
// });

// gulp.task('browserify-lib', function() {
// 	var b  = browserify({
// 		debug: true
// 	});

// 	b.add('./app/assets/lib/jquery-1.12.1.min.js');
// 	b.add('./app/assets/lib/jquery.pagination.js');
// 	//return browserify(['./app/assets/js/index.js','./app/assets/js/modules-mgr.js'])
// 	return b.bundle()
// 		.on('error', handleErrors)
// 		.pipe(source('lib.js'))
// 		.pipe(gulp.dest('./.temp/assets/js'));
// });

